class Sound {
  soundConfig = {
    1: { src: '/assets/sounds/1.mp3?ver=1.0.0', desc: '' },
    2: { src: '/assets/sounds/2.mp3?ver=1.0.0', desc: '' }
  }
  static getInstance() {
    if (!Sound.Instance) {
      Sound.Instance = new Sound()
      Sound.Instance.init()
    }
    return Sound.Instance
  }
  constructor(au = 'wc') {
    this.auth = au
  }

  init() {
    this.audio = new Audio()
    this.audio.src = this.soundConfig[1].src
    this.audio.preload = true
    document.body.appendChild(this.audio)
  }

  /**
   * @param {*} type
   */
  play(type = 1, cb = null) {
    let playSrc = this.soundConfig[type].src
    if (!playSrc) {
      console.log('播放资源不存在:type=', type)
      return
    }
    this.audio.src = this.soundConfig[type].src
    let playPromise = this.audio.play()
    // 解决 Uncaught (in promise) DOMException: The play() request was interrupted by a new load request
    if (playPromise !== undefined) {
      playPromise.then(_ => {
        // Automatic playback started!
        // We can now safely pause video...
        // this.audio.pause();
        cb && cb()
      }).catch(error => {
        // Auto-play was prevented
        // Show a UI element to let the user manually start playback
      })
    }
  }

  pause() {
    this.audio.pause();
  }

  stop() {
    this.audio.stop();
  }

  mute() {
    this.audio.muted = true;
  }
}

export default Sound.getInstance()
